﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DigitalSolutionControls
{
    public partial class Paging : UserControl
    {
        public event EventHandler PageChanged;
        private int currentPage;
        private int totalPage;

        public int TotalPage
        { 
            get
            {
                return totalPage;
            }
            set
            {
                totalPage = (value <= 0 ? 1 : value);
                lblPage.Text = currentPage + "/" + totalPage;
                UpdateButtonState();
            }
        }

        public int CurrentPage
        {
            get
            {
                return currentPage;
            }
            set
            {
                if (value > totalPage)
                    currentPage = totalPage;
                else if (value <= 0)
                    currentPage = 1;
                else
                    currentPage = value;

                lblPage.Text = currentPage + "/" + totalPage;
                UpdateButtonState();
            }
        }


        public Paging()
        {
            InitializeComponent();
            currentPage = 1;
            TotalPage = 1;
            lblPage.Text = currentPage + "/" + totalPage;
            UpdateButtonState();
        }

        private void UpdateButtonState()
        {
            btnFirst.Enabled = !(currentPage == 1 || currentPage == 2);
            btnPrevious.Enabled = !(currentPage == 1);
            btnNext.Enabled = !(currentPage == totalPage);
            btnLast.Enabled = !(currentPage == totalPage || currentPage == totalPage - 1);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currentPage++;
            if (PageChanged != null)
                PageChanged(currentPage, e);

            lblPage.Text = currentPage + "/" + totalPage;
            UpdateButtonState();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            currentPage = TotalPage;
            if (PageChanged != null)
                PageChanged(currentPage, e);

            lblPage.Text = currentPage + "/" + totalPage;
            UpdateButtonState();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            currentPage--;
            if (PageChanged != null)
                PageChanged(currentPage, e);

            lblPage.Text = currentPage + "/" + totalPage;
            UpdateButtonState();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            if (PageChanged != null)
                PageChanged(currentPage, e);

            lblPage.Text = currentPage + "/" + totalPage;
            UpdateButtonState();
        }
    }
}
